import moment from 'moment-timezone';
import {downloadBlob, downloadInstance} from '~/utils/common/download';

import http from './http';

// 直播间详情页面的图
export const getChartsForDetail = param => http.post('/api/liveMonitoring/lineChartValues', {
    ...param
});

// 总览看板
export const overallTrendDetail = param => http.post('/api/liveMonitoring/contrast', {
    ...param
});

// 获得下拉选项
export const fetchOptions = param => http.post('/api/agility/view/select', {
    ...param
});

// 直播间详情
export const roomDetail = param => http.post('/api/agility/view/values', {
    ...param
});

// 获得表格数据的统一接口
export const getTable = param => http.post('/api/liveMonitoring/excelValues', {
    ...param
});
// 获得前后5分钟事件接口
export const getAfterAndBeforeEvents = param => http.post('/api/liveMonitoring/clickShowEvent', {
    ...param
});
// 直播间列表下载
export const liveDownload = (params, name = '文件') => {
    const today = moment().format('YYYYMMDDHHmmss');
    const promise = new Promise((resolve, reject) => {
        downloadInstance
            .post('/api/liveMonitoring/excelValuesDownload', params)
            .then(res => {
                const blob = new Blob([res.data], {
                    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
                });
                downloadBlob(blob, `${name}_${today}.xlsx`);
                resolve();
            })
            .catch(error => {
                reject(error);
            });
    });
    return promise;
};
